﻿
using System;
using System.Linq;
using MS.InformationManagement.BLL.RecordCenter;
using MS.InformationManagement.Presenter.Support;
using MS.InformationManagement.DAL.RecordCenter;

namespace MS.InformationManagement.Presenter.TRDConfig
{
    public class TRDConfigPresenter : BasePresenter<ITRDConfigView>
    {
        public bool CanAddTRDConfigItem
        {
            get
            {
                return TRDBLL.CanAddTRDConfigItem(View.Backdrop, View.BackdropChild, View.Section, View.Subsection, View.Serie
                , View.Subserie);
            }
        }

        public TRDConfigPresenter(ITRDConfigView view) : base(view) { }

        public void HandleLoad()
        {
            FondosFondo fnd = new FondosFondo();
            View.Series = TRDBLL.LoadSeries();
            View.AvailableDocumentTypes = TRDBLL.LoadDocumentTypes();


            if (View.TRDItemID.HasValue)
            {
                ItemTRD entity = TRDBLL.GetItemTRD(View.TRDItemID);
                View.Backdrop = entity.Fondo;
                View.BackdropChild = entity.SubFondo;
                View.CentralStorageTime = entity.TiempoDeRetenciónEnArchivoCentral.Value;
                View.GeneralManagementStorageTime = entity.TiempoDeRetenciónEnArchivoGeneral.Value;
                View.Keep = entity.Conservación.Value;
                View.Notes = entity.Observaciones;
                View.Section = entity.SeccionTRD;
                View.Select = entity.Selección.Value;
                View.SelectedDocumentTypes = entity.TiposDocumentales.ToList();
                View.Serie = entity.SerieTRD;
                LoadSubseries();
                View.Subsection = entity.SubseccionTRD;
                View.Subserie = entity.SubserieTRD;
                View.SubserieCode = entity.SubserieTRD.Código;
                View.TechnicalMean = entity.MedioDeConservación.Value;
                View.AvailableDocumentTypes = View.AvailableDocumentTypes.Except(View.SelectedDocumentTypes,new DocumentTypeEqualityComparer()).ToList();
                View.Notes = entity.Legislación;
            }
            else
            {
                View.Subsection = TRDBLL.GetSubseccion(View.SubsectionID);
                View.Section = TRDBLL.GetSeccion(View.SectionID);
                View.Backdrop = TRDBLL.GetFondo(View.BackdropID);
                fnd = TRDBLL.GetFondo(View.BackdropChildID);
                if (fnd != null)
                    View.BackdropChild = fnd;
            }
            View.BackdropCode = View.Backdrop.Código;
            View.ChildBackdropCode = View.BackdropChild.Código;
            View.SectionCode = View.Section.Código;
            View.SubsectionCode = View.Subsection.Código;
            
            View.Show();
        }

        public void LoadSubseries()
        {
            View.SerieCode = View.Serie.Código;
            View.Subseries = TRDBLL.LoadSubseries(View.Serie.Id.Value);
            
        }

        public void SaveTRDConfigItem()
        {
            switch (View.Operation)
            {
                case "New":
                    TRDBLL.AddTRDConfigItem(string.Empty, View.Backdrop, View.BackdropChild, View.Section, View.Subsection, View.Serie
                    , View.Subserie, View.GeneralManagementStorageTime, View.CentralStorageTime, View.Keep, View.Select, View.Delete, View.TechnicalMean, View.Notes
                    , View.SelectedDocumentTypes);
                    break;
                case "View":
                    break;
                case "Edit":
                    TRDBLL.UpdateTRDConfigItem(View.TRDItemID, string.Empty, View.Backdrop, View.BackdropChild, View.Section, View.Subsection, View.Serie
                    , View.Subserie, View.GeneralManagementStorageTime, View.CentralStorageTime, View.Keep, View.Select, View.Delete, View.TechnicalMean, View.Notes
                    , View.SelectedDocumentTypes, View.ChangeNotes);
                    break;
            }
        }

        public void SetFormDependantData()
        {
            Subserie currentSubserie = View.Subserie;
            if (currentSubserie != null)
            {
                View.CentralStorageTime = currentSubserie.TiempoDeRetenciónEnArchivoCentral.Value;
                View.GeneralManagementStorageTime = currentSubserie.TiempoDeRetenciónEnArchivoGeneral.Value;
                View.Notes = currentSubserie.Legislación;
                View.Keep = currentSubserie.DisposiciónFinal.Contains("Conservar");
                View.Delete = currentSubserie.DisposiciónFinal.Contains("Eliminar");
                View.TechnicalMean = currentSubserie.DisposiciónFinal.Contains("Medio Técnico");
                View.Select = currentSubserie.DisposiciónFinal.Contains("Seleccionar");
                View.SubserieCode = currentSubserie.Código;
            }
            //{
            //    case DisposiciónFinal.Conservar:
            //        View.Keep = true;
            //        break;
            //    case DisposiciónFinal.Eliminar:
            //        View.Delete = true;
            //        break;
            //    case DisposiciónFinal.MedioTécnico:
            //        View.TechnicalMean = true;
            //        break;
            //    case DisposiciónFinal.Seleccionar:
            //        View.Select = true;
            //        break;
            //}
        }

        public void SelectAllDocumentTypes()
        {
            if (View.AvailableDocumentTypes.Count > 0)
            {
                View.SelectedDocumentTypes = View.SelectedDocumentTypes == null ? (from docType in View.AvailableDocumentTypes select docType).OrderBy(docType => docType.Title).ToList()
                    : View.SelectedDocumentTypes.Union((from docType in View.AvailableDocumentTypes select docType)).OrderBy(docType => docType.Title).ToList();
                View.AvailableDocumentTypes = new System.Collections.Generic.List<DAL.RecordCenter.TipoDocumental>();
            }
        }

        public void FromAvailableToSelected()
        {
            if (View.AvailableSelectedDocumentTypes.Count > 0)
            {
                View.SelectedDocumentTypes = View.SelectedDocumentTypes == null ? View.AvailableSelectedDocumentTypes.OrderBy(docType => docType.Title).ToList()
                    : View.SelectedDocumentTypes.Union(View.AvailableSelectedDocumentTypes).OrderBy(docType => docType.Title).ToList();
                System.Collections.Generic.List<MS.InformationManagement.DAL.RecordCenter.TipoDocumental> tempDocTypes = View.AvailableDocumentTypes;
                View.AvailableSelectedDocumentTypes.ForEach(delegate(MS.InformationManagement.DAL.RecordCenter.TipoDocumental documentalType)
                {
                    tempDocTypes.Remove(documentalType);
                });
                View.AvailableDocumentTypes = tempDocTypes;
            }
        }

        public void UnSelectAllDocumentTypes()
        {
            if (View.SelectedDocumentTypes.Count > 0)
            {
                View.AvailableDocumentTypes = View.AvailableDocumentTypes == null ? (from docType in View.SelectedDocumentTypes select docType).OrderBy(docType => docType.Title).ToList()
                    : View.AvailableDocumentTypes.Union((from docType in View.SelectedDocumentTypes select docType)).OrderBy(docType => docType.Title).ToList();
                View.SelectedDocumentTypes = new System.Collections.Generic.List<DAL.RecordCenter.TipoDocumental>();
            }
        }

        public void FromSelectedToAvailable()
        {
            if (View.SelectedSelectedDocumentTypes.Count > 0)
            {
                View.AvailableDocumentTypes = View.AvailableDocumentTypes == null ? View.SelectedSelectedDocumentTypes.OrderBy(docType => docType.Title).ToList()
                    : View.AvailableDocumentTypes.Union(View.SelectedSelectedDocumentTypes).OrderBy(docType => docType.Title).ToList();
                System.Collections.Generic.List<MS.InformationManagement.DAL.RecordCenter.TipoDocumental> tempDocTypes = View.SelectedDocumentTypes;
                View.SelectedSelectedDocumentTypes.ForEach(delegate(MS.InformationManagement.DAL.RecordCenter.TipoDocumental documentalType)
                {
                    tempDocTypes.Remove(documentalType);
                });
                View.SelectedDocumentTypes = tempDocTypes;
            }
        }

        public void MoveUp()
        {
            DAL.RecordCenter.TipoDocumental selectedDocumentype = View.SelectedSelectedDocumentTypes.FirstOrDefault();
            if (selectedDocumentype != null && selectedDocumentype != default(DAL.RecordCenter.TipoDocumental))
            {
                System.Collections.Generic.List<DAL.RecordCenter.TipoDocumental> selectedDocumentTypes = View.SelectedDocumentTypes;
                int itemIndex = selectedDocumentTypes.IndexOf(selectedDocumentype);
                if (itemIndex != 0)
                {
                    selectedDocumentTypes.Remove(selectedDocumentype);
                    selectedDocumentTypes.Insert(itemIndex - 1, selectedDocumentype);
                    View.SelectedDocumentTypes = selectedDocumentTypes;
                }
            }
        }

        public void MoveDown()
        {
            DAL.RecordCenter.TipoDocumental selectedDocumentype = View.SelectedSelectedDocumentTypes.FirstOrDefault();
            if (selectedDocumentype != null && selectedDocumentype != default(DAL.RecordCenter.TipoDocumental))
            {
                System.Collections.Generic.List<DAL.RecordCenter.TipoDocumental> selectedDocumentTypes = View.SelectedDocumentTypes;
                int itemIndex = selectedDocumentTypes.IndexOf(selectedDocumentype);
                if (itemIndex != selectedDocumentTypes.Count - 1)
                {
                    selectedDocumentTypes.Remove(selectedDocumentype);
                    selectedDocumentTypes.Insert(itemIndex + 1, selectedDocumentype);
                    View.SelectedDocumentTypes = selectedDocumentTypes;
                }
            }
        }
    }

    public class DocumentTypeEqualityComparer : System.Collections.Generic.IEqualityComparer<TipoDocumental>
    {

        public bool Equals(TipoDocumental x, TipoDocumental y)
        {
            return x.Title.Equals(y.Title) && x.Id.Equals(y.Id);
        }

        public int GetHashCode(TipoDocumental obj)
        {
            return obj.Id.GetHashCode();
        }
    }
}
